/**
 * 将平滑数据结构转化为树形结构
 * @param array
 * @param pid
 * @param idField
 * @param pidField
 * @returns {[]}
 */
export function array2Tree(array: any[], pid: string, idField = 'id', pidField = 'pid') {
  const data: any = []

  if (pid) {
    array.forEach(child => {
      if (child[pidField] === pid) {
        data.push(child)
        const children = array2Tree(array, child[idField], idField, pidField)
        if (children.length > 0) {
          child.children = children
        }
      }
    })
  } else {
    array.forEach(child => {
      const node = array.find(item => child[pidField] === item[idField])

      if (!node) {
        data.push(child)
      }
    })

    data.forEach((child: any) => {
      const children = array2Tree(array, child[idField], idField, pidField)
      if (children.length > 0) {
        child.children = children
      }
    })
  }
  return data
}
type Func = (...args: any[]) => void
export const debounce = (fn: Func, wait: number): Func => {
  let timeout: NodeJS.Timeout | null

  // 返回一个新的函数
  return function (this: any, ...args: any[]): void {
    if (timeout) {
      clearTimeout(timeout)
    }
    // 设置超时，在wait毫秒后执行fn
    timeout = setTimeout(() => {
      fn.apply(this, args)
    }, wait)
  }
}

/**
 * 将树形结构转化为平滑结构
 * @param array 树形数组
 * @param result 返回结构
 * @param childrenField children字段，默认children
 * @returns {*|Array}
 */
export function tree2Array(array: any[] = [], childrenField = 'children', result: any[] = []) {
  array.forEach(item => {
    result.push(item)
    if (item[childrenField]) {
      tree2Array(item[childrenField], childrenField, result)
    }
  })
  return result
}

/**
 * 根据ID从树形结构数据中查找自身节点
 * @param array
 * @param id
 * @param idField
 * @param childrenField
 */
export function findSelf(array = [], id?: string | null, idField = 'id', childrenField = 'children') {
  let stark: any[] = [].concat(array)

  while (stark.length) {
    const temp = stark.shift()
    if (temp[childrenField]) {
      stark = stark.concat(temp[childrenField])
    }
    if (temp[idField] === id) {
      return temp
    }
  }

  return {}
}

/**
 * 根据ID从树形结构数据中查找所有父亲节点
 * @param array
 * @param id
 * @param idField
 * @param childrenField
 */
export function findPath(array = [], id: string, idField = 'id', childrenField = 'children') {
  let result: any[] = [],
    stark: any[] = [].concat(array)

  while (stark.length) {
    const temp: any = stark.shift()
    if (temp[childrenField]) {
      stark = stark.concat(temp[childrenField])
      if (temp[childrenField].map((child: any) => child[idField]).includes(id)) {
        result = result.concat(findPath(array, temp[idField], idField))
      }
    }
    if (temp[idField] === id) {
      result.push(temp)
      break
    }
  }
  return result
}

export const findLabel = (array: any[], value: any) => (array.find(o => o.value === value) || {label: '无'}).label

// 性别
export const sexList: {
  [key: string | number]: any
} = {1: '男', 2: '女'}

// 租住年限
export const rentYears: {
  [key: string | number]: any
} = {1: '1个月', 3: '3个月', 6: '半年', 12: '1年'}

// 职业列表
export const occupation: any[] = [
  {
    label: '制造（生产/工艺/制造）',
    value: '1'
  },
  {
    label: '房产（房产/建筑）',
    value: '2'
  },
  {
    label: '金融（金融/银行/投资/保险）',
    value: '3'
  },
  {
    label: '医疗（医疗/护理/制药）',
    value: '4'
  },
  {
    label: '文化（文化/广告/传媒）',
    value: '5'
  },
  {
    label: '法律（律师/法务）',
    value: '6'
  },
  {
    label: '教育（教育/培训）',
    value: '7'
  },
  {
    label: '行政（公务员/行政/事业单位）',
    value: '8'
  },
  {
    label: '个体经营',
    value: '9'
  },
  {
    label: '贸易',
    value: '10'
  },
  {
    label: '物流',
    value: '11'
  },
  {
    label: '模特',
    value: '12'
  },
  {
    label: '空姐',
    value: '13'
  },
  {
    label: '学生',
    value: '14'
  },
  {
    label: '其它',
    value: '15'
  }
]
// 承租人收付款账号列表
export const paymentAccountList: any[] = [
  {
    label: '银联',
    value: 1
  },
  {
    label: '支付宝',
    value: 2
  },
  {
    label: '微信',
    value: 3
  }
  // {
  //   label: "美团",
  //   value: "4"
  // },
  // {
  //   label: "携程",
  //   value: "5"
  // }
]
// 同住人关系
export const renterRelationList: any[] = [
  {
    label: '配偶 ',
    value: 1
  },
  {
    label: '子女',
    value: 2
  },
  {
    label: '父母',
    value: 3
  },
  {
    label: '亲属',
    value: 4
  },
  {
    label: '合租人',
    value: 5
  },
  {
    label: '其他',
    value: 6
  }
]

export const renterRelationData: any = {
  1: '父母',
  2: '兄弟姐妹',
  3: '同事',
  4: '同学',
  5: '朋友',
  6: '亲属',
  7: '配偶',
  8: '其他'
}

// 紧急联系人关系
export const relationshipList: any[] = [
  {
    label: '父母',
    value: '1'
  },
  {
    label: '兄弟姐妹',
    value: '2'
  },
  {
    label: '同事',
    value: '3'
  },
  {
    label: '同学',
    value: '4'
  },
  {
    label: '朋友',
    value: '5'
  },
  {
    label: '亲属',
    value: '6'
  },
  {
    label: '配偶',
    value: '7'
  },
  {
    label: '其他',
    value: '8'
  }
]
// 成交方式
export const signTypeList: any[] = [
  {
    label: '新签',
    value: 1
  },
  {
    label: '续签',
    value: 2
  },
  {
    label: '转租',
    value: 3
  },
  {
    label: '换房',
    value: 4
  },
  {
    label: '补签',
    value: 5
  }
]
// 业主成交方式
export const ownerSignTypeList: any[] = [
  {
    label: '新签',
    value: 1
  },
  {
    label: '续签',
    value: 2
  }
]
// 包租合同成交方式
export const enterpriseSignTypeList: any[] = [
  {
    label: '新签',
    value: 1
  },
  {
    label: '续签',
    value: 2
  },
  {
    label: '补签',
    value: 5
  }
  // {
  // 	label: '转租',
  // 	value: 3
  // },
  // {
  // 	label: '换房',
  // 	value: 4
  // }
]
export const mortgageMonthList: any[] = [
  // {
  // 	value: 90
  // },
  {
    value: 0,
    label: '押0'
  },
  {
    value: 1,
    label: '押1'
  },
  {
    value: 2,
    label: '押2'
  },
  {
    value: 3,
    label: '押3'
  },
  {
    value: 4,
    label: '押4'
  },
  {
    value: 5,
    label: '押5'
  },
  {
    value: 6,
    label: '押6'
  },
  {
    value: 7,
    label: '押7'
  },
  {
    value: 8,
    label: '押8'
  },
  {
    value: 9,
    label: '押9'
  },
  {
    value: 10,
    label: '押10'
  },
  {
    value: 11,
    label: '押11'
  },
  {
    value: 12,
    label: '押12'
  }
]
export const periodMonthList: any[] = [
  {
    value: 1,
    label: '付1'
  },
  {
    value: 2,
    label: '付2'
  },
  {
    value: 3,
    label: '付3'
  },
  {
    value: 4,
    label: '付4'
  },
  {
    value: 5,
    label: '付5'
  },
  {
    value: 6,
    label: '付6'
  },
  {
    value: 7,
    label: '付7'
  },
  {
    value: 8,
    label: '付8'
  },
  {
    value: 9,
    label: '付9'
  },
  {
    value: 10,
    label: '付10'
  },
  {
    value: 11,
    label: '付11'
  },
  {
    value: 12,
    label: '付12'
  },
  {
    value: 99,
    label: '一次性付'
  }
]
export const paymentMethodList: any[] = [
  {
    label: '天收租',
    value: 1
  },
  {
    label: '号收租(每期第一个月)',
    value: 2
  },
  {
    label: '号收租(每期提前一个月)',
    value: 3
  }
]
export const waningMoonList: any[] = [
  {
    label: '合同月',
    value: 1
  },
  {
    label: '自然月',
    value: 2
  }
]
//企业合同目前只做合同月需求
export const moonList: any[] = [
  {
    label: '合同月',
    value: 1
  }
  // {
  // 	label: '自然月',
  // 	value: 2
  // }
]

export const customList: any[] = [
  {
    label: '不自定义',
    value: 1
  },
  {
    label: '自定义',
    value: 2
  }
]

export const periodMonthOwner: any[] = [
  // {
  // 	label: '随房租付',
  // 	value: 100
  // },
  {
    label: '一次付清',
    value: 99
  },
  {
    label: '月付',
    value: 1
  },
  // {
  // 	label: '2月付',
  // 	value: 2
  // },
  {
    label: '季付',
    value: 3
  },
  {
    label: '半年付',
    value: 6
  },
  {
    label: '年付',
    value: 12
  }
]
export const periodMonthOptions: any[] = [
  {
    label: '随房租付',
    value: 100
  },
  {
    label: '一次付清',
    value: 99
  },
  {
    label: '月付',
    value: 1
  },
  // {
  // 	label: '2月付',
  // 	value: 2
  // },
  {
    label: '季付',
    value: 3
  },
  {
    label: '半年付',
    value: 6
  },
  {
    label: '年付',
    value: 12
  }
]
